//页面加载
$.ajax({
    url: '../recipes',
    type: "POST",
    data: {
        FoodID: $("#FoodID").attr("data-FoodID")
    },
    success: function (data) {

        if (data.code == 0) {
            let Formula = JSON.parse(data.data.Formula)
            let Formula_text = ''
            for (let i in Formula) {
                Formula_text += `${i}:${Formula[i]}、`
            }
            $("#Food_data").children().eq(0).text("菜名：" + data.data.FoodName)
            $("#Food_data").children().eq(1).text("发布时间：" + data.data.Release_time.substr(0, 10))
            $("#Food_data").children().eq(2).text("收 藏 量：" + data.data.CollectionNum)
            $("#Food_data").children().eq(3).text("配料：" + Formula_text.substr(0, Formula_text.length - 1))
            $("#Food_img").attr('src', '.' + data.data.Samplefigure)
            $("#practice").text(data.data.Practice)
            if (data.data.UserID) {
                $.ajax({
                    url: '../UserData',
                    type: "POST",
                    data: {
                        UserID: data.data.UserID
                    },
                    success: function (data2) {
                        let User_data = $('#User_data').children()
                        $("#Portrait").attr('src', `../Head_p/${data.data.UserID}`)
                        User_data.eq(0).text("发布者：" + data2.result.UserName)
                        User_data.eq(1).text("发 布 数：" + data2.result.MenuNum)
                        User_data.eq(2).text("粉 丝 数：" + data2.result.FansNum)
                        console.log(data2);
                    }
                })
            }

        } else {
            let User_data = $('#User_data').children()
            User_data.eq(0).text("发布者：不详")
            User_data.eq(1).text("发 布 数：不详")
            User_data.eq(2).text("粉 丝 数：不详")
        }
    }
})

//评论留言
$("#publish").on("click", function () {
    $.ajax({
        url: '../message/insert_update',
        type: 'POST',
        data: {
            FoodID: $("#FoodID").attr("data-FoodID"),
            message: $("#floatingTextarea2").val()
        },
        success: function (data) {
            if (data.code == -1) {
                alert('请先登录')
            } else msgs()
        }
    })
})

//加载评论区
let msgs = function () {
    $.ajax({
        url: '/message/select',
        type: 'POST',
        data: {
            FoodID: $("#FoodID").attr("data-FoodID")
        },
        success: function (data) {
            let msg = JSON.parse(data.data)
            console.log(msg);
            let html=``
            for (let i in msg) {

                html+=`<a href="#" class="list-group-item list-group-item-action py-3 lh-sm" aria-current="true">
                <div class="d-flex w-100 align-items-center justify-content-between py-3">
                  <strong class="mb-1 h5">${msg[i].username}</strong>
                  <small>${msg[i].time_.substr(0, 10)}</small>
                </div>
                <div class="col-10 mb-1 small">${msg[i].msg}</div>
              </a>`
            }
            $("#comment").html(
                html
            )
        }
    })
}

msgs()